//
//  W_MineViewController.m
//  Go!
//
//  Created by 凌風 on 16/7/15.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "W_MineViewController.h"
#import "W_LoginViewController.h" // 登录注册控制器
#import "GoodsArray.h"
#import "MHCHHellpViewController.h"
#import "MHCHPersonCenterViewController.h"
#import "MHCHBaysomethingViewController.h"
#import "MHCHAboutMyViewController.h"
#import "MHCHDressViewController.h"
#import "MHCHMyorderViewController.h"
#import "ScanningViewController.h"
#import "ShareViewController.h"
#import "UIImageView+ImageWithURL.h"

@interface W_MineViewController ()
@property (nonatomic, strong) UIImageView *headView; /**< 头像 */
@property (nonatomic, strong) UIView *topView; /**< 顶部视图 */
@property (nonatomic, strong) UIButton *loginBtn; /**< 登陆按钮 */
@property (nonatomic, strong) UIView *contentView; /**< 主要界面view */
@property (nonatomic, strong) UILabel *userNameLabel;/**< 用户名标签 */

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation W_MineViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 更改self.view背景色
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self initializeDataSource];
    [self initializeUserInterface];
}

-(void)viewWillAppear:(BOOL)animated{
 self.navigationController.navigationBar.hidden = YES;
}

#pragma mark -- Initialize
- (void)initializeDataSource {
    self.navigationController.navigationBar.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToNotification:) name:@"postUserInfo" object:nil];
}

- (void)initializeUserInterface {
    [self.view addSubview:self.topView];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.headView];
    [self.view addSubview:self.contentView];
    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(30), Size_H(30)+CGRectGetMaxY(self.topView.bounds),Size_W(96), Size_H(125))btn:@"我的订单" putinimagename:@"我的订单" tag:10001]];
    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(156), Size_H(30)+CGRectGetMaxY(self.topView.bounds),Size_W(96), Size_H(125))btn:@"购物车" putinimagename:@"购物车2" tag:10002]];
    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(282), Size_H(30)+CGRectGetMaxY(self.topView.bounds),Size_W(96), Size_H(125))btn:@"收货地址" putinimagename:@"收货地址" tag:10003]];
    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(30), Size_H(180)+CGRectGetMaxY(self.topView.bounds),Size_W(96), Size_H(125))btn:@"个人中心" putinimagename:@"个人中心" tag:10004]];
    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(156), Size_H(180)+CGRectGetMaxY(self.topView.bounds),Size_W(96), Size_H(125))btn:@"帮助" putinimagename:@"帮助" tag:10005]];
    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(282), Size_H(180)+CGRectGetMaxY(self.topView.bounds),Size_W(96), Size_H(125))btn:@"关于我们" putinimagename:@"关于我们" tag:10006]];
#pragma mark - 个人拓展
    //自我添加地图扫码支付三个
//    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(30),CGRectGetMaxY(self.topView.bounds) - 50,Size_W(100), Size_H(60))btn:@"地图" putinimagename:@"" tag:10007]];
//    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(156),CGRectGetMaxY(self.topView.bounds) - 50,Size_W(100), Size_H(60))btn:@"分享" putinimagename:@"" tag:10008]];
//    [self.view addSubview:[self addimageviewWith:CGRectMake(Size_W(282),CGRectGetMaxY(self.topView.bounds) - 50,Size_W(100), Size_H(60))btn:@"扫码" putinimagename:@"" tag:10009]];
    
    
    [self.view addSubview:self.userNameLabel];
}

#pragma mark -- 监听事件的回调方法
// 登录按钮的点击事件回调方法
- (void)respondsToLoginBtn:(UIButton *)sender {
    
    if (!sender.selected) {
        W_LoginViewController *loginVc = [[W_LoginViewController alloc] init];
        
        [self presentViewController:loginVc animated:true completion:nil];
    } else {
        self.userNameLabel.text = @"";
        [self.headView setImage:[UIImage imageNamed:@"我的头像"]];
        sender.selected = false;
        sender.backgroundColor = [UIColor whiteColor];
    }
    
    
}

- (UIView *)addimageviewWith:(CGRect)frame
                         btn:(NSString *)btnname
              putinimagename:(NSString *)putinImagename
                         tag:(NSInteger) tag{
    UIView *view = [[UIView alloc]initWithFrame:frame];
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(0),view.bounds.size.width, view.bounds.size.height*3/4.0)];
    imageview.image = [UIImage imageNamed:putinImagename];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(Size_W(0),view.bounds.size.height*3/4.0,view.bounds.size.width, view.bounds.size.height*1/4.0)];
    lable.text = btnname;
    UIButton *btn = [self addbtnWithframe:view.bounds tag:tag];
    lable.textAlignment = NSTextAlignmentCenter;
    btn.backgroundColor = [UIColor clearColor];
    [view addSubview:imageview];
    [view addSubview:lable];
    [view addSubview:btn];
    return view;
}

// 通知中心的监听方法
- (void)respondsToNotification:(NSNotification *)notification {
    if ([notification.userInfo[@"user"] isEqualToString:@"loginVc"]) {
        self.userNameLabel.text = notification.object[@"userName"];
        self.loginBtn.selected = true;
        self.loginBtn.backgroundColor = [UIColor redColor];
    } else if ([notification.userInfo[@"user"] isEqualToString:@"registVc"]) {
        self.userNameLabel.text = notification.object[@"userName"];
        self.loginBtn.selected = true;
        self.loginBtn.backgroundColor = [UIColor redColor];
    } else if ([notification.userInfo[@"user"] isEqualToString:@"thirdlogin"]) {
        self.userNameLabel.text = notification.object[@"userName"];
        [self.headView setImageWithURL:notification.object[@"icon"] placeHolderImage:[UIImage imageNamed:@"我的头像"]];
        self.loginBtn.selected = true;
        self.loginBtn.backgroundColor = [UIColor redColor];
    }
}

-(UIButton *)addbtnWithframe:(CGRect)frame tag:(NSInteger)tage{
    UIButton *btn = [[UIButton alloc]initWithFrame:frame];
    btn.tag = tage;
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(void)clickBtn:(UIButton *)sender{
    if (sender.tag == 10001) {
        MHCHMyorderViewController *myorderVC = [[MHCHMyorderViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if (sender.tag == 10002){
        MHCHBaysomethingViewController *myorderVC = [[MHCHBaysomethingViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if (sender.tag == 10003){
        MHCHDressViewController *myorderVC = [[MHCHDressViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if (sender.tag == 10004){
        MHCHPersonCenterViewController *myorderVC = [[MHCHPersonCenterViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if (sender.tag == 10005){
        MHCHHellpViewController *myorderVC = [[MHCHHellpViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if(sender.tag == 10006){
        MHCHAboutMyViewController *myorderVC = [[MHCHAboutMyViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if(sender.tag == 10007){
        MHCHAboutMyViewController *myorderVC = [[MHCHAboutMyViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if(sender.tag == 10008){
        ShareViewController *myorderVC = [[ShareViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }else if(sender.tag == 10009){
        ScanningViewController *myorderVC = [[ScanningViewController alloc]init];
        [self.navigationController pushViewController:myorderVC animated:YES];
    }

}


#pragma mark -- Getter
// 登录按钮
- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.bounds = CGRectMake(0, 0, Size_W(150), Size_H(40));
        _loginBtn.center = CGPointMake(ScreenW * 0.5, Size_H(230));
        
        _loginBtn.backgroundColor = [UIColor whiteColor];
        [_loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
        [_loginBtn setTitle:@"注 销" forState:UIControlStateSelected];
        
        [_loginBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        _loginBtn.layer.masksToBounds = true;
        _loginBtn.layer.cornerRadius = 3;
        
        [_loginBtn addTarget:self action:@selector(respondsToLoginBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _loginBtn;
}

- (UIImageView *)headView{
    if (!_headView) {
        _headView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.loginBtn.frame), Size_H(40),Size_W(150), Size_H(150))];
        
        _headView.layer.masksToBounds = true;
        _headView.layer.cornerRadius = Size_W(150) * 0.5;
        _headView.image  = [UIImage imageNamed:@"我的头像"];
    }
    return _headView;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(0),Size_W(414), Size_H(300))];
        _topView.backgroundColor = [UIColor orangeColor];
        
    }
    return _topView;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(Size_W(0), Size_H(300),Size_W(414), Size_H(736-300)-49)];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

- (UILabel *)userNameLabel {
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(Size_W(8), Size_H(30), Size_W(150), Size_H(40))];
//        _userNameLabel.backgroundColor = [UIColor whiteColor];
        _userNameLabel.textColor = [UIColor redColor];
    }
    
    return _userNameLabel;
}




@end
